use ratatui::{
    Frame,
    layout::{Constraint, Direction, Layout, Rect},
    widgets::{Block, Borders, Paragraph},
};

use crate::form::FormState;

use super::{
    fields::render_fields,
    sections::{render_root_tabs, render_section_tabs},
};

pub fn render_body(
    frame: &mut Frame<'_>,
    area: Rect,
    form_state: &mut FormState,
    enable_cursor: bool,
) {
    if form_state.is_empty() {
        let placeholder = Paragraph::new("No editable fields in schema")
            .block(Block::default().borders(Borders::ALL));
        frame.render_widget(placeholder, area);
        return;
    }

    let root_tabs_view = form_state.root_tabs_view();
    let show_root_tabs = root_tabs_view
        .as_ref()
        .map(|view| view.titles.len() > 1)
        .unwrap_or(false);
    let section_tabs_view = form_state.section_tabs_view();
    let show_section_tabs = section_tabs_view
        .as_ref()
        .map(|view| view.titles.len() > 1)
        .unwrap_or(false);

    let mut constraints = Vec::new();
    if show_root_tabs {
        constraints.push(Constraint::Length(3));
    }
    if show_section_tabs {
        constraints.push(Constraint::Length(3));
    }
    constraints.push(Constraint::Min(1));

    let chunks = Layout::default()
        .direction(Direction::Vertical)
        .constraints(constraints)
        .split(area);

    let mut index = 0;
    if show_root_tabs {
        if let Some(view) = root_tabs_view.as_ref() {
            render_root_tabs(frame, chunks[index], view);
        }
        index += 1;
    }
    if show_section_tabs {
        if let Some(view) = section_tabs_view.as_ref() {
            render_section_tabs(frame, chunks[index], view);
        }
        index += 1;
    }
    render_fields(frame, chunks[index], form_state, enable_cursor);
}
